package com.lantosec.hmrp.modules.internal.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.internal.entity.InternalType;
import com.lantosec.hmrp.modules.internal.service.IInternalTypeService;
import com.lantosec.hmrp.modules.internal.vo.InternalTypeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 *
 * 项目分类表 控制器
 *
 * @author AutoCode
 * @since 2020-04-27
 */
@Api(value = "项目分类表控制器", tags = "项目分类表控制器")
@RestController
@RequestMapping("/internal/type")
@Log4j2
public class InternalTypeController extends BaseController {
    @Autowired
    private IInternalTypeService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<InternalTypeVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<Page<InternalTypeVO>> page(@ApiParam(value = "分页对象", required = true) PageVO<InternalType> page,
                                             @ApiParam(value = "项目分类名称") @RequestParam(name = "internalTypeName",required = false) String internalTypeName){
        page = service.page(page,new QueryWrapper<InternalType>().like(internalTypeName != null,InternalType.ColName.internal_type_name.toString(),internalTypeName)
                                                                    .orderByAsc(InternalType.ColName.type_order.toString()));
        return Result.ok(entity2VO(page));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<InternalTypeVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ControllerLog(name = "新增内部满意度项目分类", type = LogType.add)
    @ApiOperation(value = "新增内部满意度项目分类")
    public Result<Integer> create(@ApiParam(value = "项目分类表VO对象", required = true) @RequestBody @Validated InternalTypeVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        InternalType entity = vo.convertToEntity();
        service.save(entity);
        return Result.ok(entity.getInternalTypeId());
    }

    @PutMapping("update")
    @ControllerLog(name = "修改内部满意度项目分类", type = LogType.update)
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "项目分类表VO对象", required = true) @RequestBody InternalTypeVO vo){
        InternalType entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ControllerLog(name = "逻辑删除内部满意度项目分类", type = LogType.delete)
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }
}
